package com.scalar.forecast.columnDisplay.actionCommands;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.users.User;
import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.columnDisplay.dao.SearchMetaDataDAO;
import com.scalar.forecast.columnDisplay.forms.SearchMetaDataForm;
import com.scalar.forecast.commons.SessionKeys;

/**
 * Processes the request to show the display details of the search screen
 *
 * Created by Veeru. 
 * Date: Feb 16, 2005
 * Time: 11:04:54 AM 
 */


public class SearchMetaDataEditCommand extends ActionCommand {

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        HttpSession session =  request.getSession();
        ActionForward actionForward = mapping.findForward( "editColumnDisplay" );
        SearchMetaDataForm SearchMetaDataForm = ( SearchMetaDataForm )form;

        HashMap errors = new HashMap ();
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        String tableName = null;

        if ( request.getParameter("tableName") != null  )
        {
            tableName = request.getParameter("tableName");
            SearchMetaDataForm.setTableName( tableName );
            try
            {
                User pUser = ( User )session.getAttribute( SessionKeys.USER_KEY );
                String userId = pUser.getUserId();
                if ( !SearchMetaDataDAO.isUserHasEntries ( tableName, userId ) )
                {
                    SearchMetaDataDAO.saveEntriesWithUserId ( null, tableName, userId );
                }
                SearchMetaDataForm.setColumnMetaDataList( SearchMetaDataDAO.loadMetaData ( null, tableName, userId ) );
            }
            catch( Exception e )
            {
                errors.put( "Error: Could not load column display details", null );
            }
        }
        return actionForward;
    }
}
